package com.qzsoft.zhjd.asphalt.modules.asphaltdata.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.dto.AsphaltDataDetailsDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.dto.DayDataStatisticsDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.dto.ProduceAsphaltDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.dto.ReportCountDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.entity.AsphaltDataEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.service.AsphaltDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.ResultUtils;

/**
 * @ClassName AsphaltDataController
 * @Description 沥青数据控制器
 * @author linhuawei
 * @since 2021-11-23
 */
@RestController
@RequestMapping("/api/asphaltData")
public class AsphaltDataController {

    @Autowired
    private AsphaltDataService asphaltDataService;


    /**
     * 设备数据上传
     */
    @PostMapping("/dataBaseUpload")
    public String dataBaseUpload(@RequestBody JSONObject json){
        Long key = null;
        JSONObject result = new JSONObject();
        try {
            if ("LQBHZ".equals(json.getString("deviceType"))){
                key = asphaltDataService.saveDataBaseUpload(json);
            }else if ("XGLQBHZ".equals(json.getString("deviceType"))){
                // 徐工集团版本数据接收接口
                key = asphaltDataService.saveDataBaseUploadXg(json);
            } else if ("XGLQBHZSB".equals(json.getString("deviceType"))){
                // 徐工集团版本数据接收接口三标
                key = asphaltDataService.saveDataBaseUploadXgsb(json);
            } else if ("YBLQBHZ".equals(json.getString("deviceType"))){
                // 徐工集团版本数据接收接口
                key = asphaltDataService.saveDataBaseUploadYb(json);
            }else if ("MLNLQBHZ".equals(json.getString("deviceType"))){
                // 玛连尼版本数据接收接口 济微四标沥青拌合站，sql-server
                key = asphaltDataService.saveDataBaseUploadMln(json);
            } else if ("TXGB".equals(json.getString("deviceType"))){
                // 泰新过磅数据接收
                key = asphaltDataService.saveDataBaseUploadTxgb(json);
            }
            else {
                result.put("code", 300);
                return result.toJSONString();
            }
            result.put("code", 200);
            result.put("data", key+"");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("沥青拌合站数据上传错误："+e);
            result.put("code", 300);
        }
        return result.toJSONString();
    }


    /**
     * @Title produceAsphalt
     * @Description 油石比检测
     * @param params 查询条件
     * @return 生产重量
     */
    @RequiresPermissions("api:asphaltData:produceAsphaltAggregate")
    @GetMapping("/produceAsphaltAggregate")
    public Result<List<ProduceAsphaltDto>> produceAsphalt(@RequestParam Map<String, Object> params) {
        List<ProduceAsphaltDto> list = asphaltDataService.produceAsphalt(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title dayDateStatistics
     * @Description 全天数据统计
     * @param params 查询条件
     * @return 全天数据统计
     */
    @RequiresPermissions({"api:asphaltData:requestTime","api:asphaltData:alldayrequestTime","api:asphaltData:productmoninter"})
    @GetMapping("/produceDayDateStatistics")
    public Result<DayDataStatisticsDto> dayDataStatistics(@RequestParam Map<String, Object> params) {
        DayDataStatisticsDto data = asphaltDataService.dayDataStatistics(params);
        return ResultUtils.ok(data);
    }

    /**
     * @Title details
     * @Description 获取数据详情
     * @param id 数据主键
     * @return 数据详情
     */
    @GetMapping("/details/{id}")
    public Result<AsphaltDataDetailsDto> details(@PathVariable("id") String id){
        AsphaltDataDetailsDto data = asphaltDataService.details(id);
        return ResultUtils.ok(data);
    }

    /**
     * @Title reportCount
     * @Description 沥青混合料生产数据报表统计
     * @param params 查询条件
     * @return 沥青混合料生产数据报表统计
     */
    @RequiresPermissions("api:asphaltData:reportcount")
    @GetMapping("/reportCount")
    public Result<List<ReportCountDto>> reportCount(@RequestParam Map<String, Object> params){
        List<ReportCountDto> list = asphaltDataService.reportCount(params);
        return ResultUtils.ok(list);
    }

    /**
     * 获取所有有数据得时间
     * @return
     */
    @RequiresPermissions({"api:asphaltData:requestTime","api:asphaltData:alldayrequestTime"})
    @GetMapping("/requestTime")
    public Result<List<String>> requestTime(@RequestParam Map<String,Object> params){
        List<String> data = asphaltDataService.requestTime(params);
        return ResultUtils.ok(data);
    }

    /**
     * 根据级配类型获取生产配比
     * @param type
     * @return
     */
    @GetMapping("/getMatchCode")
    public Result<List<Map<String, Object>>> getMatchCode(String type){
        List<Map<String, Object>> data = asphaltDataService.getMatchCode(type);
        return ResultUtils.ok(data);
    }

    /**
     * 获取生产配比
     * @return
     */
    @GetMapping("/getInitMatchCode")
    public Result<List<Map<String, Object>>> getInitMatchCode(){
        List<Map<String, Object>> data = asphaltDataService.getInitMatchCode();
        return ResultUtils.ok(data);
    }

    /**
     * @Title getLatestData
     * @Description 获取最新数据
     * @return 最新数据
     */
    @GetMapping("/getLatestData")
    public Result<AsphaltDataEntity> getLatestData(@RequestParam Map<String,Object> params){
        AsphaltDataEntity entity = asphaltDataService.getLatestData(params);
        return ResultUtils.ok(entity);
    }

    /**
     * @Title getProduceTime
     * @Description 获取生产时间
     * @param params 查询条件
     * @return 生产时间列表
     */
    @GetMapping("/getProduceTime")
    public Result<List<String>> getProduceTime(@RequestParam Map<String,Object> params){
        List<String> data = asphaltDataService.getProduceTime(params);
        return ResultUtils.ok(data);
    }
}
